//
//  RoundDotToViewController.m
//  ADreamClusive
//
//  Created by ADreamClusive on 17/6/23.
//  Copyright © 2017年 Jiaozl. All rights reserved.
//

#import "RoundDotToViewController.h"
#import "ZLTransitionDelegate.h"
#import "ZLInteractiveTransition.h"
#import "RoundDotAnimation.h"

@interface RoundDotToViewController ()

@property(nonatomic,strong) UIButton *finishBtn;
@property (nonatomic, strong) ZLTransitionDelegate *transitionDelegate;
@property (nonatomic, strong) RoundDotAnimation *roundDotAnimation;
@property (nonatomic, strong) UIScreenEdgePanGestureRecognizer* gestureRecognizer;

@end

@implementation RoundDotToViewController

- (instancetype)init
{
    if (self = [super init] ) {
        self.transitioningDelegate = self.transitionDelegate;
        self.modalPresentationStyle = UIModalPresentationCustom;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor =[UIColor whiteColor];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"123.jpg"]];
    imageView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    imageView.layer.cornerRadius = 10;
    imageView.layer.masksToBounds = YES;
    [self.view addSubview:imageView];
    
    [self.view addSubview:self.finishBtn];
    [self setupGesture];
}

- (void)setupGesture
{
    // 添加屏幕边缘滑动手势
    UIPanGestureRecognizer *screenEdgePanGR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(_actionLeftSwipe:)];
    [self.view addGestureRecognizer:screenEdgePanGR];
}

- (void)_actionLeftSwipe:(UIScreenEdgePanGestureRecognizer *)sender
{
    [self _actionPresentNewVC:sender];
}

- (void)_actionPresentNewVC:(id)sender
{
    if ([sender isKindOfClass:UIGestureRecognizer.class] ) {
        if (((UIGestureRecognizer *)sender).state == UIGestureRecognizerStateBegan) {
            [self _actionDismiss:sender];
        }
    } else {
        [self _actionDismiss:sender];
    }
}

- (IBAction)_actionDismiss:(id)sender
{
    if ([sender isKindOfClass:UIGestureRecognizer.class] ) {
        self.gestureRecognizer = sender;
    } else {
        self.gestureRecognizer = nil;
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)_actionDismiss
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

// MARK: - getter
- (UIButton *)finishBtn
{
    if (!_finishBtn) {
        _finishBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _finishBtn.frame = CGRectMake(138,323,100, 20);
        [_finishBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_finishBtn setTitle:@"完成" forState:UIControlStateNormal];
        [_finishBtn addTarget:self action:@selector(_actionDismiss) forControlEvents:UIControlEventTouchUpInside];
    }
    return _finishBtn;
}

- (ZLTransitionDelegate *)transitionDelegate
{
    if (!_transitionDelegate) {
        _transitionDelegate = [[ZLTransitionDelegate alloc] init];
        weakify(self);
        _transitionDelegate.animatedPresentTransition = ^id<UIViewControllerAnimatedTransitioning> _Nonnull{
            strongify(self);
            return self.roundDotAnimation;
        };
        _transitionDelegate.animatedDismissalTransition = ^id<UIViewControllerAnimatedTransitioning> _Nonnull{
            strongify(self);
            return self.roundDotAnimation;
        };
        _transitionDelegate.interactiveDismissalTransition = ^id<UIViewControllerInteractiveTransitioning> _Nonnull{
            strongify(self);
            if (self.gestureRecognizer) {
                return [[ZLInteractiveTransition alloc] initWithGestureRecognizer:self.gestureRecognizer edgeForDragging:UIRectEdgeTop];
            }
            return nil;
        };
    }
    return _transitionDelegate;
}

- (RoundDotAnimation *)roundDotAnimation
{
    if (!_roundDotAnimation) {
        _roundDotAnimation = [[RoundDotAnimation alloc] init];
    }
    return _roundDotAnimation;
}

@end
